﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Lancetinc.Evs.Reports;

/// <summary>
/// 报告术语
/// </summary>
public class ReportTerminology
{
    public const string GroupPrefix = "@";

    /// <summary>
    /// 分组名称
    /// </summary>
    public string GroupName { get; set; }

    /// <summary>
    /// 术语
    /// </summary>
    public string[] Terms { get; set; }

    /// <inheritdoc />
    public override string ToString()
    {
        var sb = new StringBuilder();
        sb.Append(GroupPrefix).Append(GroupName).AppendLine();
        foreach (var term in Terms)
        {
            sb.Append(term).AppendLine();
        }

        return sb.ToString().Trim();
    }

    public static ReportTerminology Parse(string[] lines)
    {
        if (lines.Length == 0)
            return null;

        var groupLine = lines[0].Trim();
        if (!groupLine.StartsWith(GroupPrefix))
        {
            return null;
        }

        var term = new ReportTerminology();
        term.GroupName = groupLine.Substring(1);
        var list = new List<string>();
        foreach (var line in lines.Skip(1))
        {
            var l = line.Trim();
            if (string.IsNullOrEmpty(l))
                continue;
            list.Add(l);
        }
        term.Terms = list.ToArray();
        return term;
    }
}